import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.nio.channels.Selector;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {

    public static void main(String[] args) throws InterruptedException, IOException {
//        test01();
//        test02();
//        test03(); //会报错，因为点击的是百度的新闻超链接，这个超链接不能submit
//        test04();
//        test05();
//        test06();
//        test07();//浏览器操作
//        test08();
//        test09();
//        page01();
//        page03();//下拉框选择
//        page04();//针对alert的操作，弹窗
//        page05();//上传文件
//        test10();
//        test11();
        test12();


    }

    private static void test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //截图
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("d://20240107.png"));


    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
//        webDriver.getWindowHandles();//获得所有的窗口句柄
//        webDriver.getWindowHandle();//获得get打开的页面窗口句柄
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:handles){
            System.out.println(handle);
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);//webDriver切到新的页面了
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();




    }

    private static void test10() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(7)")).click();
        sleep(5000);
        webDriver.close();



    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium_test_01/src/main/Page/test05.html?_ijt=tsddat7g527cg5bnl914fmqsj3");
        webDriver.findElement(By.cssSelector("input")).sendKeys("F:\\桌面\\PS\\03 矢量图层\\素材作业\\抠图");
        webDriver.quit();//关闭浏览器

    }

    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium_test_01/src/main/Page/test04.html?_ijt=ms1v8all2aik40fkacp6hhufn3");
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        //alert弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        //点击按钮
        webDriver.findElement(By.cssSelector("button")).click();
        //在弹窗中输入jzh
        webDriver.switchTo().alert().sendKeys("jzh");
        //alert弹窗的确认
        sleep(3000);
        webDriver.switchTo().alert().accept();

    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium_test_01/src/main/Page/test03.html?_ijt=lhpmqc0pt67uucvqc5ko0c1n2m");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        select.selectByValue("12.51");


    }

    private static void page01() {



    }

    private static void test09() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //找到对应的图片按钮
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        //鼠标右击
//        sleep(3000);
        Actions actions = new Actions(webDriver);
        actions.moveToElement(element).contextClick().perform();


    }

    private static void test08() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        sleep(3000);
        //ctrl+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        //ctrl+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        //ctrl+V
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");


    }

    private static void test07() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        //强制等待3秒
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        //刷新
        sleep(3000);
        webDriver.navigate().refresh();
        //强制等待3秒
        sleep(3000);
        //浏览器前进
        webDriver.navigate().forward();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(300,300));


    }

    private static void test06() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com/");




        WebDriverWait webDriverWait = new WebDriverWait(webDriver,3);
        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div")));
        webDriverWait.until(ExpectedConditions.titleIs("百度一下，你就知道"));


    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
//        System.out.println(url);
//        System.out.println(title);
        if (url.equals("https://www.baidu.com/") && title.contains("百度一下")) {
            System.out.println("当前页面："+url);
            System.out.println("title："+title);
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }



    }

    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com");
        String text = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (text.equals("百度一下")){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
            System.out.println(text);
        }
//        System.out.println(text);


    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();


    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com");
        //搜索输入框中输入 java
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
        //点击百度一下
        webDriver.findElement(By.cssSelector("#su")).submit();
//        sleep(3000000);
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);//隐式等待三天
        WebDriverWait wait = new WebDriverWait(webDriver,3000);



        //清空搜索框的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();


    }

    private static void test01() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度的首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //在输入框中输入软件测试
        element.sendKeys("软件测试");

        //找到百度一下这个按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for(int i = 0;i<elements.size();i++){
//            System.out.println(elements.get(i).getText());
            if (elements.get(i).getText().contains("测试") || elements.get(i).getText().contains("软件")){
                System.out.println("测试通过");
            }else{
                System.out.println("测试不通过");
            }

        }

    }






}



















